package com.example.day01_homework3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class HomeAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<Bean.DataBean> list=new ArrayList<>();

    public HomeAdapter(Context context) {
        this.context = context;
    }


    public void setRefresh(ArrayList<Bean.DataBean> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    public void setLoadMore(ArrayList<Bean.DataBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item, parent, false);
        return new HomeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HomeHolder homeHolder= (HomeHolder) holder;
        homeHolder.title.setText(list.get(position).getTitle());
        Glide.with(context).load(list.get(position).getPic()).into(homeHolder.iv);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class HomeHolder extends RecyclerView.ViewHolder {

        private final ImageView iv;
        private final TextView title;

        public HomeHolder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.item_iv);
            title = itemView.findViewById(R.id.item_title);
        }
    }
}
